<?php
namespace app\common\service;

use think\facade\Request;

class Base {

    public $CODE_SUCCESS = 200;
    public $CODE_NO_PARAM = 201;
    public $CODE_FAIL = 202;

    /**
     * @name: 回调
     * @Param: 
     * @Description: 
     * @Author: foo
     * @Date: 2019-10-09 16:34:44
     */
    function toDo ($action, $params = [], $return = [])
    {
        if ($this->getErrorCode() != $this->CODE_SUCCESS)
        {
            $this->setError($this->getErrorCode(), $this->getErrorMsg());
            return false;
            return $return;
        }

        $returnClassMethod = is_array($action) ? $action : [$this, $action];


        if (!call_user_func_array('method_exists', $returnClassMethod))
        {
            $this->setError($this->CODE_FAIL, '不存在这个方法');
            return $return;
        }
        return call_user_func_array($returnClassMethod, $params);
    }

    /**
     * @name: 获取主机信息 (数组)
     * @Param: 
     * @Description: 
     * @Author: foo
     * @Date: 2019-11-15 13:59:08
     */
    public function getHost ($field = '', $type = 'array')
    {
        global $online_host;
        $return = $online_host;

        if(!empty($field))
        {
            $return = array_column($return, $field, 'id');
        }
        
        if ($type == 'string')
        {
            $return = implode(',', $return);
        }

        return $return;
        
    }

    /**
     * @name: 设置错误信息
     * @Param: 
     * @Description: 
     * @Author: foo
     * @Date: 2019-05-22 16:44:38
     */
    function setError ($code, $msg)
    {
        global $errCode;
        $errCode = $code;
        global $errMsg;
        $errMsg = $msg;
    }

    /**
     * @name: 获取错误码
     * @Param: 
     * @Description: 
     * @Author: foo
     * @Date: 2019-05-22 16:44:18
     */
    function getErrorCode ()
    {
        global $errCode;
        return $errCode;
    }

    /**
     * @name: 获取错误信息
     * @Param: 
     * @Description: 
     * @Author: foo
     * @Date: 2019-05-22 16:43:48
     */
    function getErrorMsg ()
    {
        global $errMsg;
        return $errMsg;       
    }

    /**
     * @name: 获取登陆用户信息
     * @Param: 
     * @Description: 
     * @Author: foo
     * @Date: 2019-10-09 23:47:13
     */
    function getUserValue ($index = '')
    {
        global $userInfo;

        return $userInfo[$index];
    }

    /**
     * @name: 调用异步
     * @Param: 
     * @Description: 
     * @Author: foo
     * @Date: 2019-11-29 13:34:54
     */
    public function Async($module, $method, $parmas =[])
    {
        global $errCode;
        if ($errCode != $this->CODE_SUCCESS)
        {
            return false;
        }

        $server = new \app\adminapiv3\service\Base();
        $user = $server->getUserInfo();

        $url = $_SERVER['REQUEST_SCHEME'].'://' . $_SERVER['HTTP_HOST']  . '/async/v3/callClient';

        $data = [
            'module' => $module,
            'action' => $method,
            'params' => json_encode($parmas),
            'user_id' => $user['user_id'],
            'host_id' => $this->getHost('id', 'string'),
        ];

        curlPost($url, $data, 2);
    }

    /**
     * @name: 主机输出语句
     * @Param: 
     * @Description: 
     * @Author: foo
     * @Date: 2019-12-24 15:35:57
     */
    function hostErrorMessage ($host_id_a, $message)
    {
        $host_a = $this->getHost();
        $host_name_a = [];
        foreach ($host_id_a as $host_id)
        {
            !isset($host_a[$host_id]) ? : $host_name_a[] = $host_a[$host_id]['name'] ;
        }

        $return =  '主机' . '['. implode(']，[', $host_name_a) . ']' . $message;

        return $return;
    }
}